package adapter

import (
	"ginfwk/infrastructure/component/queue"
	"math/rand"
	"strconv"
	"time"
)

type Message struct {
	// 队列属性
	Category      string
	BaseQueueName string
	QueueName     string

	// 参数
	Route  string
	Params interface{}

	// 消息属性
	MsgId     string
	DelayTime time.Duration

	// 消息发送属性
	MsgHeaders map[string]interface{}
	Exchange   string
	RoutingKey string
}

func NewMessage(queuePrefix, queueName string, route string, param interface{}, option *queue.Options) *Message {
	// 队列名称
	prefix := queuePrefix
	if len(option.Category) > 0 {
		prefix = option.Category
	}
	prefixedQueueName := prefix + queueName

	// 消息Id
	datetime := time.Now().Format("20060102150405")
	msgId := strconv.Itoa(int(rand.Uint32()))

	// 延时参数
	delayTime := time.Duration(0)
	if option.DelayTime > 0 {
		delayTime = option.DelayTime
	}

	return &Message{
		Category:      prefix,
		BaseQueueName: queueName,
		QueueName:     prefixedQueueName,
		Route:         route,
		MsgId:         prefix + "-" + datetime + "-" + msgId,
		MsgHeaders:    make(map[string]interface{}),

		Params:    param,
		DelayTime: delayTime,
	}
}
